<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: CategoryService.cfc 1144 2007-05-06 17:05:53Z mpwoodward $

Notes:
--->
<cfcomponent
	displayname="CategoryService"
	output="false"
	hint="CategoryService for MachBlog">

	<!---
	PROPERTIES
	--->
	<cfproperty name="categoryGateway" type="CategoryGateway" required="true" />
	<cfproperty name="categoryDAO" type="CategoryDAO" required="true" />
	<cfproperty name="userFacade" type="machblog.org.machblog.facades.UserFacade" required="true" />
	<cfset variables.instance = StructNew() />

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="init" access="public" output="false" returntype="CategoryService" 
		hint="Initializes the service.">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setDevMode" access="public" returntype="void" output="false">
		<cfargument name="devMode" required="true" type="string" />
		<cfset variables.instance.devMode = arguments.devMode />
	</cffunction>
	<cffunction name="getDevMode" access="private" returntype="string" output="false">
		<cfreturn variables.instance.devMode />
	</cffunction>

	<cffunction name="setCategoryGateway" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="categoryGateway" type="CategoryGateway" required="true" />
		<cfset variables.categoryGateway = arguments.categoryGateway />
	</cffunction>	
	<cffunction name="getCategoryGateway" access="private" output="false" returntype="CategoryGateway">
		<cfreturn variables.categoryGateway />
	</cffunction>
	
	<cffunction name="setCategoryDAO" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="categoryDAO" type="CategoryDAO" required="true" />
		<cfset variables.categoryDAO = arguments.categoryDAO />
	</cffunction>
	<cffunction name="getCategoryDAO" access="private" output="false" returntype="CategoryDAO">
		<cfreturn variables.categoryDAO />
	</cffunction>
	
	<cffunction name="setUserFacade" access="public" output="false" returntype="void"
		hint="Dependency: injected">
		<cfargument name="userFacade" type="machblog.org.machblog.facades.UserFacade" required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>	
	<cffunction name="getUserFacade" access="private" output="false" returntype="machblog.org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>
	
	<cffunction name="getCategoryBean" access="public" output="false" returntype="Category">
		<cfreturn createObject("component", "Category").init() />
	</cffunction>

	<!---
	GATEWAY RELATED
	--->
	<cffunction name="getCategories" access="public" output="false" returntype="query" 
			hint="Returns a query object containing categories, either all or active only depending on the boolean passed in.">
		<cfargument name="includeInactive" type="boolean" required="false" default="false" />
		<cfreturn getCategoryGateway().getCategories(arguments.includeInactive) />
	</cffunction>
	
	<cffunction name="getCategoriesAsArray" access="public" output="false" returntype="array" 
			hint="Returns an array containing categories, either all or active only depending on the boolean passed in">
		<cfargument name="includeInactive" type="boolean" required="false" default="false" />
		
		<cfset var categoryQuery = getCategories(arguments.includeInactive) />
		<cfset var category = StructNew() />
		<cfset var categories = ArrayNew(1) />
		
		<cfif categoryQuery.recordCount gt 0>
			<cfloop query="categoryQuery">
				<cfset category = StructNew() />
				<cfset category.categoryId = categoryQuery.category_id />
				<cfset category.categoryName = categoryQuery.category_name />
				<cfset category.numPosts = categoryQuery.num_posts />
				<cfset arrayAppend(categories, category) />
			</cfloop>
		</cfif>
		
		<cfreturn categories />
	</cffunction>
	
	<!---
	DAO RELATED
	--->
	<cffunction name="createCategory" access="public" output="false" returntype="void" hint="Creates a category">
		<cfargument name="category" type="Category" required="true" />
		
		<cfset getCategoryDAO().create(arguments.category) />
	</cffunction>
	
	<cffunction name="getCategory" access="public" output="false" returntype="Category"
		hint="Retrieves a category by ID">
		<cfargument name="categoryId" type="string" required="true" />
		
		<cfset var getCategory = "" />
		<cfset var cbName = "" />
		<cfset var mbName = "" />
		<cfset var dtModified = 0 />
		<cfset var category = getCategoryBean() />
			
		<cftry>
			<cfset getCategory = getCategoryDAO().read(arguments.categoryId) />
			<cfcatch type="database">
				<cfthrow type="application" message="Database Error" detail="#CFCATCH.Detail#" />
			</cfcatch>
		</cftry>

		<!--- If we got a hit, populate the object --->
		<cfif isQuery(getCategory) AND getCategory.RecordCount EQ 1>
			<cfset cbName = getCategory.cbfn & " " & getCategory.cbln />
				
			<!--- handle the modified by stuff --->
			<cfif getCategory.modified_by_id NEQ "">
				<cfset mbName = getCategory.mbfn & " " & getCategory.mbln />
				<cfset dtModified = getCategory.dt_modified />
			</cfif>
			
			<cfset category.init(getCategory.category_id, getCategory.category_name, 
										getCategory.num_posts) />
			<cfset category.getAudit().init(getCategory.created_by_id, cbName, 
												getCategory.dt_created, getCategory.ip_created, 
												getCategory.modified_by_id, mbName, 
												dtModified, getCategory.ip_modified, 
												getCategory.is_active) />
		</cfif>

		<cfreturn category />
	</cffunction>
	
	<cffunction name="updateCategory" access="public" output="false" returntype="void" hint="Updates a category">
		<cfargument name="category" type="Category" required="true" />

		<cfset getCategoryDAO().update(arguments.category) />
	</cffunction>
	
	<cffunction name="deleteCategory" access="public" output="false" returntype="void" hint="Deletes a category">
		<cfargument name="category" type="Category" required="true" />

		<cfset getCategoryDAO().delete(arguments.category) />
	</cffunction>
</cfcomponent>